package com.wd.mylibrary_live.viewmodel

import android.view.View
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.blankj.utilcode.util.GsonUtils
import com.wd.mylibrary_base.bean.CloseLiveBean
import com.wd.mylibrary_base.bean.OpenLiveBean
import com.wd.mylibrary_base.bean.PutBean
import com.wd.mylibrary_base.bean.WebsocketData
import com.wd.mylibrary_base.socket.WebSocketUtils
import com.wd.mylibrary_base.utils.HttpUtils
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody
import org.json.JSONObject
import java.io.File

/**
 * 作者: 邢凌志
 * 时间: 2024/9/19
 * 描述: LiveViewModel
 */
class LiveViewModel : ViewModel() {
    var liveId = 0
    var agoraToken = ""
    var channelName = ""
    var compositeWsUrl = ""

    //    liveCoverImage	file	直播封面照	文件流	是
    //开直播
    var openLiveData= MutableLiveData<OpenLiveBean>()
    fun postOpenLive(userId:Int,sessionId:String,liveTitle:String, file: File){
        val body = RequestBody.create("multipart/form-data".toMediaTypeOrNull(), file)
        val part: MultipartBody.Part = MultipartBody.Part.createFormData("liveCoverImage", file.name, body)
        HttpUtils.httpUtils.apiService.postOpenLive(userId,sessionId,
            101,liveTitle,"116.4074","39.3042",part)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe {
                openLiveData.value=it
            }
    }
    //开始推流
    var startPushStreamData= MutableLiveData<PutBean>()
    fun startPushStream(
        userId:Int,sessionId:String,liveId:Int
    ){
        HttpUtils.httpUtils.apiService.startPushStream(userId, sessionId, liveId)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe {
                startPushStreamData.value=it
            }
    }
    //关播
    var closeLiveData= MutableLiveData<CloseLiveBean>()
    fun closeLive(userId:Int,sessionId:String){
        HttpUtils.httpUtils.apiService.closeLive(userId, sessionId)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe {
                closeLiveData.value=it
            }
    }

//    private var webSocketUtils: WebSocketUtils? = null
//    val mLiveBase = MutableLiveData<WebsocketData.LiveBase?>()
//    val mPersonCount = MutableLiveData<WebsocketData.PersonCount?>()
//    val mBridge = MutableLiveData<WebsocketData.Bridge?>()
//    val mConcat = MutableLiveData<WebsocketData.Concat?>()
//    fun connectSocket(context: Context) {
//        //开启ws连接
//        webSocketUtils = WebSocketUtils().startConnect(compositeWsUrl, context)
//        //消息监听
//        webSocketUtils!!.setOnMessageListener(object : WebSocketUtils.OnMessageListener {
//            override fun message(msg: String?) {
//                Log.e("======connectSocked okhttp", msg.toString())
//
//                msg ?: return
//                when (JSONObject(msg).getInt("msgType")) {
//                    //基础信息
//                    101 -> {
//                        mLiveBase.value =
//                            GsonUtils.fromJson(msg, WebsocketData.LiveBase::class.java)
//                    }
//                    //直播间实时人数更新消息
//                    102 -> {
//                        mPersonCount.value =
//                            GsonUtils.fromJson(msg, WebsocketData.PersonCount::class.java)
//                    }
//                    //鹊桥类消息
//                    103 -> {
//                        mBridge.value = GsonUtils.fromJson(msg, WebsocketData.Bridge::class.java)
//                    }
//                    //连麦类消息
//                    104 -> {
//                        mConcat.value = GsonUtils.fromJson(msg, WebsocketData.Concat::class.java)
//                    }
//                }
//            }
//
//        })
//    }


}








